package com.liuyang.service.impl;

import cn.hutool.core.map.MapBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuyang.entity.News;
import com.liuyang.mapper.NewsMapper;
import com.liuyang.service.NewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者:caleb
 * @since 2022-03-02
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public Map<String, Object> getNewsInfo(int page, int limit) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<News>();
        Page<News> newsPage = new Page<>(page, limit);
        IPage<News> newsIPage = newsMapper.selectPage(newsPage, queryWrapper);
        long count = newsIPage.getTotal();
        List<News> list = newsIPage.getRecords();
        MapBuilder<String, Object> mapBuilder = MapBuilder.create();
        Map<String, Object> map = mapBuilder.put("count", count).put("list", list).build();
        return map;
    }
}
